package com.securitytrace.util;

import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

public abstract class AddressConvertUtil {
	
	private static Properties properties;
	static {
		properties = new Properties();
		try {
			properties.load(AddressConvertUtil.class.getClassLoader().getResourceAsStream("addressSeparator.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String vectorToString(Vector<Vector<String>> objects) {
		String str = "";
		for(Vector<String> vector : objects) {
			String key = vector.get(0);
			String value = vector.get(1);
			if(null == key || value == null || 0 >= key.length() || 0 >= value.length()) {
				return null;
			}else if(key.trim().isEmpty()||value.trim().isEmpty()) {
				return null;
			}else {
				str += key + properties.getProperty("KV") + value + properties.getProperty("SS");
			}
			
		}
		return str;
	}
	//[[asd, asd], [asd, asd], [asd, asd]]
	public static Vector<Vector<String>> stringToVector(String str) {
		str = str.substring(2, str.length()-2);
		str = str.replaceAll(", ", properties.getProperty("KV"));
		str = str.replaceAll("\\]: \\[", properties.getProperty("SS"));
		str = str.trim();
		return standardStringToVector(str);
	}
	//asd: asd; asd: asd; asd: asd
	public static Vector<Vector<String>> standardStringToVector(String str) {
		Vector<Vector<String>> vectors = new Vector<Vector<String>>();
		str = str.substring(0, str.lastIndexOf(properties.getProperty("SS")));
		String[] strs = str.split(properties.getProperty("SS"));
		for(String str1 : strs) {
			String[] st = str1.split(properties.getProperty("KV"));
			Vector<String> vector = new Vector<String>();
			for(String s : st) {
				vector.add(s.trim());
			}
			vectors.add(vector);
		}
		return vectors;
	}
}
